package cn.edu.hbvtc.qms.web.struts.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.edu.hbvtc.qms.domain.Course;
import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.domain.Major;
import cn.edu.hbvtc.qms.domain.Role;
import cn.edu.hbvtc.qms.domain.Teacher;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.service.IManagementService;
import cn.edu.hbvtc.qms.web.struts.form.CourseActionForm;

public class CourseAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到课程管理列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获得当前登录用户
		User user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		if (user.getRoles().contains(role)) {
			Teacher teacher = managementService.findTeacherByUserId(user
					.getId());
			// 首先找出院系管理员所在院系的专业列表
			List<Major> majorList = managementService.findMajorsOfDept(teacher
					.getDept());
			// 定义clazzListAll，用来保存所有专业下的班级
			List<Course> courseListAll = new ArrayList<Course>();
			// 通过遍历专业列表，找出各专业下的班级信息
			for (Iterator<Major> majorIter = majorList.iterator(); majorIter
					.hasNext();) {
				Major major = majorIter.next();
				List<Course> courseList = managementService
						.findCoursesOfMajor(major);
				courseListAll.addAll(courseList);
			}
			request.setAttribute("courseList", courseListAll);
		} else {
			List<Course> courseList = managementService.findCourses();
			request.setAttribute("courseList", courseList);
		}
		return mapping.findForward("list");
	}

	// 跳转到课程添加页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseActionForm caf = (CourseActionForm) form;
		User user = managementService.findUserByUsername(this.getPrincipal()
				.getUsername());
		Role role = managementService.findRoleByName("DEPT_ADMIN");
		if (user.getRoles().contains(role)) {
			Teacher teacher = managementService.findTeacherByUserId(user
					.getId());
			Department dept = teacher.getDept();
			request.setAttribute("dept", dept);

			List<Major> majorList = managementService.findMajorsOfDept(teacher
					.getDept());
			request.setAttribute("majorList", majorList);

		} else {
			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			Department dept = managementService.findDepartmentById(caf
					.getDeptId());
			List<Major> majorList = managementService.findMajorsOfDept(dept);
			request.setAttribute("majorList", majorList);
		}

		return mapping.findForward("add");
	}

	// 完成课程添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseActionForm caf = (CourseActionForm) form;
		
		if (caf.getCourseId() == null || "".equals(caf.getCourseId())) {
			request.setAttribute("courseIdError", "课程编号不能为空！");
			return mapping.findForward("toAdd");
		}else if(caf.getCourseId().length()!=6){
			request.setAttribute("courseIdLength", "请输入长度为6位的课程编号！");
			return mapping.findForward("toAdd");
		}else if(managementService.findCourseByCourseId(caf.getCourseId())!=null){
			request.setAttribute("courseIdRequire","课程编号已注册，请核对后重新输入！");
			return mapping.findForward("toAdd");
		}
		
		if (caf.getCourseName() == null || "".equals(caf.getCourseName())) {
			request.setAttribute("courseNameError", "课程名称不能为空！");
			return mapping.findForward("toAdd");
		}
		if (caf.getHours() == 0 || "".equals(caf.getHours())) {
			request.setAttribute("courseHoursError", "课程学时数不能为空！");
			return mapping.findForward("toAdd");
		}
		if (caf.getCredit() == 0 || "".equals(caf.getCredit())) {
			request.setAttribute("courseCreditError", "课程学分不能为空！");
			return mapping.findForward("toAdd");
		}
		if (caf.getCourseType() == 0) {
			request.setAttribute("courseTypeError", "课程类型不能为空！");
			return mapping.findForward("toAdd");
		}
		if (caf.getCourseType() == 2) {
			if (caf.getDeptId() != 0 || caf.getMajorId() != 0) {
				request.setAttribute("deptAndMajorError", "公共课不能设置所属院系和专业！");
				return mapping.findForward("toAdd");
			}
		} else if (caf.getCourseType() == 1) {
			if (caf.getDeptId() == 0) {
				request.setAttribute("courseDeptIdError", "所属院系不能为空！");
				return mapping.findForward("toAdd");
			}
			if (caf.getMajorId() == 0) {
				request.setAttribute("courseMajorIdError", "所属专业不能为空！");
				return mapping.findForward("toAdd");
			}
		}
		Course course = new Course();
		PropertyUtils.copyProperties(course, caf);

		if (caf.getMajorId() == 0) {
			course.setMajor(null);
		} else {
			Major major = managementService.findMajorById(caf.getMajorId());
			course.setMajor(major);
		}
		managementService.saveCourse(course);

		return mapping.findForward("toList");
	}

	// 跳转到课程修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseActionForm caf = (CourseActionForm) form;
		Course course = managementService.findCourseById(caf.getId());
		PropertyUtils.copyProperties(caf, course);
		request.setAttribute("course", course);
		if (course.getMajor() != null) {
			
			caf.setMajorId(course.getMajor().getId());
			
			Department dept = managementService.findDepartmentById(course
					.getMajor().getDept().getId());
			
			List<Major> majorList = managementService.findMajorsOfDept(dept);
			request.setAttribute("majorList", majorList);
		}

		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("update");
	}

	// 保存课程修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseActionForm caf = (CourseActionForm) form;
		
		if (caf.getCourseId() == null || "".equals(caf.getCourseId())) {
			request.setAttribute("courseIdError", "课程编号不能为空！");
			return mapping.findForward("toUpdate");
		}
		if (caf.getCourseName() == null || "".equals(caf.getCourseName())) {
			request.setAttribute("courseNameError", "课程名称不能为空！");
			return mapping.findForward("toUpdate");
		}
		if (caf.getHours() == 0 || "".equals(caf.getHours())) {
			request.setAttribute("courseHoursError", "课程学时数不能为空！");
			return mapping.findForward("toUpdate");
		}
		if (caf.getCredit() == 0 || "".equals(caf.getCredit())) {
			request.setAttribute("courseCreditError", "课程学分不能为空！");
			return mapping.findForward("toUpdate");
		}
		if (caf.getCourseType() == 0) {
			request.setAttribute("courseTypeError", "课程类型不能为空！");
			return mapping.findForward("toUpdate");
		}
		if (caf.getCourseType() == 2) {
			if (caf.getDeptId() != 0 || caf.getMajorId() != 0) {
				request.setAttribute("deptAndMajorError", "公共课不能设置所属院系和专业！");
				return mapping.findForward("toUpdate");
			}
		} else if (caf.getCourseType() == 1) {
			if (caf.getDeptId() == 0) {
				request.setAttribute("courseDeptIdError", "所属院系不能为空！");
				return mapping.findForward("toUpdate");
			}
			if (caf.getMajorId() == 0) {
				request.setAttribute("courseMajorIdError", "所属专业不能为空！");
				return mapping.findForward("toUpdate");
			}
		}
		Course course = managementService.findCourseById(caf.getId());
		PropertyUtils.copyProperties(course, caf);

		if (caf.getMajorId() == 0) {
			course.setMajor(null);
		} else {
			Major major = managementService.findMajorById(caf.getMajorId());
			course.setMajor(major);
		}
		managementService.updateCourse(course);

		return mapping.findForward("toList");
	}

	// 完成课程删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CourseActionForm caf = (CourseActionForm) form;
		managementService.deleteCourseById(caf.getId());

		return mapping.findForward("delete");
	}
}
